/*
 * Copyright 2011 Kim Lindhardt Madsen
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package dk.lindhardt.gwt.geie.client.edit;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.user.client.ui.*;
import dk.lindhardt.gwt.geie.client.image.GeieImages;
import dk.lindhardt.gwt.geie.shared.Cell;

import java.util.ArrayList;
import java.util.List;

/**
 * User: AnAmuser
 * Date: 17-08-11
 * <p/>
 * Dialog for editing properties in the cell.
 */
public class GUICellEditor extends DialogBox {

   private static GeieImages imageResources = GWT.create(GeieImages.class);

   private List<Cell> cells;
   private ValueEditor.EditHandler editHandler;
   private ValueEditor valueEditor;

   private VerticalPanel mainPanel = new VerticalPanel();
   private List<Menu> menus = new ArrayList<Menu>();

   public GUICellEditor(List<Cell> cells, ValueEditor valueEditor, ValueEditor.EditHandler editHandler) {
      super(false, false);
      this.valueEditor = valueEditor;
      this.cells = cells;
      this.editHandler = editHandler;

      if (cells.size() == 1) {
         Cell cell = cells.get(0);
         setText(cell.getPosition().toString());
      } else {
         Cell firstCell = cells.get(0);
         Cell lastCell = cells.get(cells.size() - 1);
         setText(firstCell.getPosition().toString() + ":" + lastCell.getPosition().toString());
      }

      //Exit editor when user presses escape
      addDomHandler(
            new KeyDownHandler() {
               public void onKeyDown(KeyDownEvent event) {
                  int code = event.getNativeKeyCode();
                  if (KeyCodes.KEY_ESCAPE == code) {
                     GUICellEditor.this.editHandler.onEditCancelled();
                  }
               }
            }, KeyDownEvent.getType());

      add(mainPanel);
   }

   /**
    * Sets the value editor being used in the cell editor
    * @param valueEditor the value editor
    */
   public void setValueEditor(ValueEditor valueEditor) {
      this.valueEditor = valueEditor;
   }

   /**
    * SGets the value editor being used in the cell editor
    * @return the value editor
    */
   public ValueEditor getValueEditor() {
      return valueEditor;
   }

   /**
    * Get cell being edited by the valueEditor
    * @return the cell being edited by the valueEditor
    */
   public Cell getEditedCell() {
      return cells.get(0);
   }

   /**
    * set cell being edited by the valueEditor
    * @param cell the cell being edited by the valueEditor
    */
   public void setEditedCell(Cell cell) {
      cells.set(0, cell);
   }

   /**
    * Get all the cells being edited by this editor
    * @return all the cells being edited by this editor
    */
   public List<Cell> getCells() {
      return cells;
   }

   /**
    * Is the editor listening for clicks on the table
    * @return is the editor listening for clicks on the table
    */
   public boolean isListeningForTableClicks() {
      return valueEditor.isListeningForTableSelections();
   }

   /**
    * Handles selections on the table
    * @param cells the cell that was selected
    */
   public void handleTableSelection(List<Cell> cells) {
      valueEditor.handleTableSelection(cells);
   }

   /**
    * Gets the style menus being displayed by this editor
    * @return the style menus
    */
   public List<StyleMenu> getStyleMenus() {
      List<StyleMenu> result = new ArrayList<StyleMenu>();
      for (Menu menu : menus) {
         if (menu instanceof StyleMenu) {
            result.add((StyleMenu) menu);
         }
      }
      return result;
   }

   /**
    * Adds a menu to this editor. Menus are displayed in the order that they are added.
    * @param menu  the menu to be added
    */
   public void addMenu(Menu menu) {
      menus.add(menu);
   }

   public void redrawContent() {
      mainPanel.clear();
      HorizontalPanel stylePanel = new HorizontalPanel();
      for (Menu styleMenu : menus) {
         stylePanel.add(styleMenu.asWidget());
      }
      mainPanel.add(stylePanel);
      mainPanel.add(new HTML("<hr/>"));
      HorizontalPanel valuePanel = new HorizontalPanel();
      valuePanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
      valuePanel.setSpacing(5);
      valuePanel.add(new Image(imageResources.functionOfX()));
      valuePanel.add(valueEditor.getInputWidget());
      mainPanel.add(valuePanel);
   }

   /**
    * Redraws the content and shows the editor
    */
   @Override
   public void show() {
      redrawContent();
      super.show();
   }
}
